package com.github.zdongcoding.recyclerviewlib;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.github.zdg.recyclerview.BaseRecyclerAdapter;
import com.github.zdg.recyclerview.ItemViewHolder;
import com.github.zdg.recyclerview.RecyclerListAdapter;
import com.github.zdg.recyclerview.itemdecoration.Edge;
import com.github.zdg.recyclerview.itemdecoration.GridEdgeDividerDecoration;
import com.github.zdg.recyclerview.itemdecoration.LinearEdgeDividerDecoration;
import com.github.zdongcoding.recyclerviewlib.model.Item;
import com.github.zdongcoding.recyclerviewlib.model.ItemTitle;
import com.github.zdongcoding.recyclerviewlib.viewholder.TitleViewHolder;


public abstract class SimpleActivity extends AppCompatActivity {

    protected RecyclerView mRecyclerView;
    protected LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    protected GridLayoutManager mGridLayoutManager = new GridLayoutManager(this, 2);
    protected RecyclerListAdapter mRecyclerListAdapter;
    protected LinearEdgeDividerDecoration mLinearDividerDecoration;
    protected GridEdgeDividerDecoration mGridEdgeDividerDecoration;
    private int orientation= 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.base_recycler_layout);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mLinearDividerDecoration = new LinearEdgeDividerDecoration(this, LinearEdgeDividerDecoration.VERTICAL, ContextCompat.getDrawable(this, R.drawable.bg_animal_divider));
        mLinearDividerDecoration.setEdge(new Edge(Edge.RIGHT_EDGE|Edge.LEFT_EDGE));
        mGridEdgeDividerDecoration = new GridEdgeDividerDecoration(this,1,R.drawable.bg_animal_divider,R.drawable.bg_animal_h_divider);
        initView();
//        mRecyclerListAdapter.addViewType(4, new BaseRecyclerAdapter.ViewHolderFactory<ItemViewHolder>() {
//            @Override
//            public ItemViewHolder onCreateViewHolder(ViewGroup parent) {
//                return new TitleViewHolder(parent);
//            }
//        });
        initData();
    }

    abstract void initView();

    abstract void initData();
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
//            case R.id.menu_layout_manger:
//                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
//                    mRecyclerView.setLayoutManager(mLinearLayoutManager);
//                    item.setTitle("GridLayoutManager");
//                } else {
//                    mRecyclerView.setLayoutManager(mGridLayoutManager);
//                    item.setTitle("LinearLayoutManager");
//                }
//                break;
            case R.id.menu_left:
                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    mGridEdgeDividerDecoration.getEdge().addEdge(Edge.LEFT_EDGE);
                } else {
                    mLinearDividerDecoration.getEdge().addEdge(Edge.LEFT_EDGE);
                }

                break;
            case R.id.menu_right:
                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    mGridEdgeDividerDecoration.getEdge().addEdge(Edge.RIGHT_EDGE);
                } else {
                    mLinearDividerDecoration.getEdge().addEdge(Edge.RIGHT_EDGE);
                }
                break;
            case R.id.menu_top:
                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    mGridEdgeDividerDecoration.getEdge().addEdge(Edge.TOP_EDGE);
                } else {
                    mLinearDividerDecoration.getEdge().addEdge(Edge.TOP_EDGE);
                }
                break;
            case R.id.menu_bottom:
                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    mGridEdgeDividerDecoration.getEdge().addEdge(Edge.BUTTOM_EDGE);
                } else {
                    mLinearDividerDecoration.getEdge().addEdge(Edge.BUTTOM_EDGE);
                }
                break;
            case R.id.menu_clear:
                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    mGridEdgeDividerDecoration.setEdge(new Edge(Edge.NO_EDGE));
                } else {
                    mLinearDividerDecoration.setEdge(new Edge(Edge.NO_EDGE));
                }
                break;
            case R.id.menu_orientation:
                if (orientation == 1) {
                    orientation = 0;
                    mLinearDividerDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.bg_animal_h_divider));
                    item.setTitle("HORIZONTAL");
                } else {
                    orientation = 1;
                    mLinearDividerDecoration.setDrawable(ContextCompat.getDrawable(this,R.drawable.bg_animal_divider));
                    item.setTitle("VERTICAL");
                }
                if (mRecyclerView.getLayoutManager() instanceof GridLayoutManager) {
                    mGridLayoutManager.setOrientation(orientation);
                    mGridEdgeDividerDecoration.setOrientation(orientation);
                    mGridEdgeDividerDecoration.setEdge(new Edge(Edge.NO_EDGE));
                } else {
                    mLinearLayoutManager.setOrientation(orientation);
                    mLinearDividerDecoration.setOrientation(orientation);
                    mLinearDividerDecoration.setEdge(new Edge(Edge.NO_EDGE));
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
